package servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.codec.digest.DigestUtils;

import pojo.EUserType;
import tools.Tool;
import utils.db.impl.UserAdd;
import utils.db.impl.UserFetcher;

/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		
		HttpSession session=null;
		String email = request.getParameter("email");
		String password = DigestUtils.md5Hex(request.getParameter("password"));
		String confirmPassword =  DigestUtils.md5Hex(request.getParameter("confirmPassword"));
		String name = request.getParameter("name");
		String surname = request.getParameter("surname");
		int phone = Integer.parseInt(request.getParameter("phone"));
		EUserType type = EUserType.CLIENT;
		
		if(email!=null && password!=null && confirmPassword!=null && name!=null && surname!=null && phone!=0){
			if(password.equals(confirmPassword)){
				if(Tool.checkEmailFormat(email)){
					if(Tool.checkPhoneNumber(phone)){
						UserFetcher us = new UserFetcher();
						if(!us.checkUserExist(email)){ 
							UserAdd add = new UserAdd();
							add.adduser(name, surname, password, email, type, phone);
							session = request.getSession(true);
							response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
							response.setHeader("Location", "index.jsp?page=registerSuccess#pages");
						}else{
							
							session = request.getSession(true);
							session.setAttribute("warning", "Użytkownik o podanym adresie email już istnieje.<br> " +
									"Jeżeli nie pamiętasz hasła możesz je <a href=index.jsp?page=forgotPassword>przypomnieć</a>");
						    response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
						    response.setHeader("Location", "index.jsp?page=register#pages");
						}
					}else{
						session = request.getSession(true);
						session.setAttribute("warning", "Błedny numer telefonu!");
					    response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
					    response.setHeader("Location", "index.jsp?page=register#pages");
					}
				}else{
					session = request.getSession(true);
					session.setAttribute("warning", "Hasła się nie zgadzają!");
				    response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
				    response.setHeader("Location", "index.jsp?page=register#pages");
				}
			}else{
				session = request.getSession(true);
				session.setAttribute("warning", "Hasła się ze sobą nie zgadzają!");
			    response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
			    response.setHeader("Location", "index.jsp?page=register#pages");
			}
		}else{
			session = request.getSession(true);
			session.setAttribute("warning", "Niektóre pola pozostały puste!");
		    response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
		    response.setHeader("Location", "index.jsp?page=register#pages");
		}
	}

}
